package com.sinodata.bsm.center.web.action.config;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.service.config.ProtocolParameterService;
import com.sinodata.bsm.center.service.config.SystemParameterService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.ProtocolParameter;
import com.sinodata.bsm.common.vo.SystemParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-10 下午3:00:02    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ConfigAction extends BaseAction<ProtocolParameter> {

    @Autowired
    private ProtocolParameterService protocolParameterService;

    @Autowired
    private SystemParameterService systemParameterService;

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7893925802375666008L;

    /**
     * 通用页面返回frame
     * 
     * @return
     */
    public String common() {
        return "frame";
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public ProtocolParameter getModel() {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#list()
     */
    @Override
    public String list() throws Exception {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#save()
     */
    @Override
    public String save() throws Exception {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#delete()
     */
    @Override
    public String delete() throws Exception {

        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#prepareModel()
     */
    @Override
    protected void prepareModel() throws Exception {

    }

    /**
     * 采集协议列表
     * @return
     */
    public String protocolParameters() {
        String type = Struts2Utils.getParameter("type");
        List<ProtocolParameter> paramers = protocolParameterService.findByType(type);
        Struts2Utils.renderJson(paramers);
        return null;

    }

    public String viewSystemParameter() {
        List<SystemParameter> systemParameters = systemParameterService.findAll();
        Struts2Utils.getRequest().setAttribute("allParameters", systemParameters);
        return "parameter-view";
    }

    public String updateParameter() {
        List<SystemParameter> list = systemParameterService.findAll();
        if (list == null || list.size() < 1) {
            return null;
        }
        Map<String, SystemParameter> map = new HashMap<String, SystemParameter>(list.size());
        for (SystemParameter e : list) {
            e.setValue("0");
            map.put(e.getKey(), e);
        }
        Enumeration<String> parameters = Struts2Utils.getRequest().getParameterNames();
        while (parameters.hasMoreElements()) {
            String key = parameters.nextElement();
            String value = Struts2Utils.getRequest().getParameter(key);
            SystemParameter entity = new SystemParameter(key, value);
            map.put(entity.getKey(), entity);
        }
        List<SystemParameter> updateList = new ArrayList<SystemParameter>(map.values());
        systemParameterService.update(updateList);
        return null;
    }
}
